package rise.net.handler;

import java.util.List;

import rise.comunity.Community;
import rise.comunity.Status;
import rise.comunity.User;
import rise.message.CommunityStatusNotificaton;
import rise.ui.Conversation;
import rise.ui.MapActivity;
import rise.ui.UserListActivity;
import android.util.Log;

public class CommunityStatusNotificationHandler implements Runnable {
	private String packetRappresentation;

	public CommunityStatusNotificationHandler(String packetRappresentation) {
		this.packetRappresentation = packetRappresentation;
	}

	@Override
	public void run() {
		CommunityStatusNotificaton communityStatusNotificaton = CommunityStatusNotificaton
				.createCommunityStatusNotificaton(packetRappresentation);
		
		List<User> users = communityStatusNotificaton.getUsers();
		for (User user : users) {
			
			String username = user.getUsername();
			Status userStatus = user.getStatus();
			double lat = user.getGPSLocation().getLat(), lon = user.getGPSLocation().getLon();
			
			Community community = Community.obtainCommunity();
			if (!community.contains(username))
				community.addUser(new User(userStatus, username, lat, lon));
			community.setUserStatus(username, userStatus);
			community.setUserGPSLocation(username, lat, lon);
			
			UserListActivity.handler.sendEmptyMessage(0);
			Conversation.handler.sendEmptyMessage(0);
			MapActivity.handler.sendEmptyMessage(0);
		}
	}

}
